/**
 * 
 */
package db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Adresse;
import model.Datum;
import model.Kunde;
import model.Mitarbeiter;
import model.Name;
import model.Person;

/**
 * @author spiller.sebastian
 *
 */
public class PersonBuilder implements PersonSource{
	
	private static DbManager DbManagerInstance = DbManager.getInstance();
	private static PersonBuilder PersonBuilderInstance;
	
	private PersonBuilder(){
	}
	
	/**
	 * @return
	 */
	public static PersonBuilder getInstance() {
		if (PersonBuilder.PersonBuilderInstance == null) {
			PersonBuilder.PersonBuilderInstance = new PersonBuilder();
		}
		return PersonBuilder.PersonBuilderInstance;
	}
	
	/**
	 * @return
	 */
	public List<Person> getAllPerson() {
		
		try {
			ResultSet rs = DbManagerInstance.getCon().createStatement()
				.executeQuery("SELECT * FROM t_person" +
					" LEFT JOIN t_name ON name_id = t_name.id" +
					" LEFT JOIN t_adresse ON adresse_id = t_adresse.id" +
					" LEFT JOIN t_mitarbeiter ON t_person.id = t_mitarbeiter.person_id" +
					" LEFT JOIN t_kunde ON t_person.id = t_kunde.person_id");
			
			
			List<Person> listPerson= new ArrayList<Person>();

			while (rs.next()) {
				
				Name name = new Name(
							rs.getString("anrede"), 
							rs.getString("vorname"),
							rs.getString("nachname"));
				
				Adresse adresse = new Adresse(
									rs.getString("strasse"),
									rs.getString("hausnummer"),
									rs.getString("plz"),
									rs.getString("ort"));
				
				Datum geburtsdatum = new Datum(rs.getDate("geburtsdatum"));
	
				if ( rs.getString("abteilung") == null || rs.getString("abteilung").isEmpty()) {

					Kunde person = new Kunde(
						name,
						adresse,
						rs.getString("telefonnr"),
						geburtsdatum,
						rs.getInt("kundennr"),
						rs.getDouble("bonus"));
					
					listPerson.add(person);
				}
				else {
					Mitarbeiter person = new Mitarbeiter(
						name,
						adresse,
						rs.getString("telefonnr"),
						geburtsdatum,
						rs.getInt("personalnr"),
						rs.getString("abteilung"),
						rs.getString("gehaltsstufe"));
					
					listPerson.add(person);
				}
			}
			
			return listPerson;
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void add(Person person) {
		// TODO Next iteration
	}
	
	public void update(Person person) {
		// TODO Next iteration
	}
	
	public void remove(Person person) {
		// TODO Next iteration		
	}

	public Kunde getKunde(int int1) {
		
		Kunde kunde = null;
		
		try {
			ResultSet rs = DbManagerInstance.getCon().createStatement()
				.executeQuery("SELECT * FROM t_person" +
					" LEFT JOIN t_name ON name_id = t_name.id" +
					" LEFT JOIN t_adresse ON adresse_id = t_adresse.id" +
					" LEFT JOIN t_kunde ON t_person.id = t_kunde.person_id" +
					" WHERE kundennr ="+int1);
			
			rs.next();
			
			Name name = new Name(
					rs.getString("anrede"), 
					rs.getString("vorname"),
					rs.getString("nachname"));
			
			Adresse adresse = new Adresse(
								rs.getString("strasse"),
								rs.getString("hausnummer"),
								rs.getString("plz"),
								rs.getString("ort"));
			
			Datum geburtsdatum = new Datum(rs.getDate("geburtsdatum"));
			
			kunde = new Kunde(
					name,
					adresse,
					rs.getString("telefonnr"),
					geburtsdatum,
					rs.getInt("kundennr"),
					rs.getDouble("bonus"));
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return kunde;
	}

	public Mitarbeiter getMitarbeiter(int int1) {
		// TODO Next iteration
		return null;
	}
}